package com.t.screenofftouch.service;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.Service;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;

import com.t.screenofftouch.CommonFunctions;
import com.t.screenofftouch.Constants;
import com.t.screenofftouch.MainActivity;
import com.t.screenofftouch.R;
import com.t.screenofftouch.SplashActivity;
@SuppressWarnings("deprecation")
public class BubbleService extends Service implements OnTouchListener {

	private static final int ALPHA_IMG = 0;
	private LayoutParams params;
	private ImageView bubbleView;
	private WindowManager windowManager;
	
	private DevicePolicyManager devicePolicyManager;
	private PowerManager mPowerManager;
	private WindowManager mWindowManager;
	private WakeLock mWakeLock;

	protected static final int REQUEST_CODE_ENABLE_ADMIN = 1;
	private Context context;
	private SharedPreferences pref;
	private Editor editor;
	
	@Override
	public IBinder onBind(Intent intent) {
	
		return null;
	}
	@Override
	public void onCreate() {
		super.onCreate();
		this.context =  getApplicationContext();
		windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
		bubbleView = new ImageView(this);
		bubbleView.setAlpha(120);
		pref = getApplicationContext().getSharedPreferences("screenoff", MODE_PRIVATE); 
		editor = pref.edit();
		loadicon();
		try {
			Constants.WIDTH =getWidth();
			Constants.X=getX();
			Constants.Y=getY();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		//	params.gravity = Gravity.TOP | Gravity.LEFT;
		params = new LayoutParams(Constants.WIDTH,
				Constants.WIDTH,Constants.X,Constants.Y, LayoutParams.TYPE_PHONE,
				LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);
		windowManager.addView(bubbleView, params);
		bubbleView.setOnTouchListener(this);
		devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
	}

	private float oldX;
	private float oldY;
	private int paramOldX;
	private int paramOldY;
	private void loadicon(){
		switch(geticon()){
		case 1:
			bubbleView.setImageResource(R.drawable.icon1);
			break;
		case 2:
			bubbleView.setImageResource(R.drawable.icon2);
			break;
		case 3:
			bubbleView.setImageResource(R.drawable.icon3);
			break;
		case 4:
			bubbleView.setImageResource(R.drawable.icon4);
			break;
		case 5:
			bubbleView.setImageResource(R.drawable.icon5);
			break;
		case 6:
			bubbleView.setImageResource(R.drawable.icon6);
			break;
		case 7:
			bubbleView.setImageResource(R.drawable.icon7);
			break;
		case 8:
			bubbleView.setImageResource(R.drawable.icon8);
			break;
		case 9:
			bubbleView.setImageResource(R.drawable.icon9);
			break;
		case 10:
			bubbleView.setImageResource(R.drawable.icon10);
			break;
		case 11:
			bubbleView.setImageResource(R.drawable.icon11);
			break;
		case 12:
			bubbleView.setImageResource(R.drawable.icon12);
			break;
		case 13:
			bubbleView.setImageResource(R.drawable.icon13);
			break;
		case 14:
			bubbleView.setImageResource(R.drawable.icon14);
			break;
		case 15:
			bubbleView.setImageResource(R.drawable.icon15);
			break;
		case 16:
			bubbleView.setImageResource(R.drawable.icon16);
			break;
		case 17:
			bubbleView.setImageResource(R.drawable.icon17);
			break;
		case 18:
			bubbleView.setImageResource(R.drawable.icon18);
			break;
		case 19:
			bubbleView.setImageResource(R.drawable.icon19);
			break;
		case 20:
			bubbleView.setImageResource(R.drawable.icon20);
			break;
		case 21:
			bubbleView.setImageResource(R.drawable.icon21);
			break;
		case 22:
			bubbleView.setImageResource(R.drawable.icon22);
			break;
		case 23:
			bubbleView.setImageResource(R.drawable.icon23);
			break;
		case 24:
			bubbleView.setImageResource(R.drawable.icon24);
			break;
		case 25:
			bubbleView.setImageResource(R.drawable.icon25);
			break;
		case 26:
			bubbleView.setImageResource(R.drawable.icon26);
			break;
		default:
			bubbleView.setImageResource(R.drawable.icon4);
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if (v == bubbleView) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				paramOldX = params.x;
				paramOldY = params.y;
				oldX = event.getRawX();
				oldY = event.getRawY();
				bubbleView.setAlpha(255);
				handler.removeMessages(ALPHA_IMG);
				break;
			case MotionEvent.ACTION_UP:
				writefile(Constants.WIDTH, params.x,params.y);
				if(Math.abs(event.getRawX() - oldX) < 5){
					if(!CommonFunctions.isActiveAdmin(context))
					{
						Intent intentMain  = new Intent(context,SplashActivity.class);
						intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						startActivity(intentMain);
					}
					else{
						try{

				        // Get an instance of the PowerManager
							mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
	
				        // Get an instance of the WindowManager
							mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
							mWindowManager.getDefaultDisplay();
	
				        // Create a bright wake lock
							mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
				     	
							devicePolicyManager.lockNow();
				        	mWakeLock.acquire();
							mWakeLock.release();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}
				handler.sendEmptyMessageDelayed(ALPHA_IMG, 2000);
				break;
			case MotionEvent.ACTION_MOVE:
				params.x = paramOldX + (int) (event.getRawX() - oldX);
				params.y = paramOldY + (int) (event.getRawY() - oldY);
				
				windowManager.updateViewLayout(bubbleView, params);
				break;
			default:
				break;
			}
		}

		return true;
	}
	
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			bubbleView.setAlpha(120);
		}

	};

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (bubbleView != null) {
			windowManager.removeView(bubbleView);
		}
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}
	public void writefile(int width, int x, int y){
		try {
			editor.remove("width");
			editor.remove("X");
			editor.remove("Y");
			editor.commit();
			editor.putInt("width", width);       
		    editor.putInt("X", x); 
		    editor.putInt("Y", y);
		    editor.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public  int getWidth(){
		return pref.getInt("width", 50);
	}
	public  int getX(){
		return pref.getInt("X", 0);
	}
	public  int getY(){
		return pref.getInt("Y", 0);
	}
	public  int geticon(){
		return pref.getInt("icon", 4);
	}
}
